package test01;

// ProcessFilter.java - 统计单词数量
import java.util.HashMap;
import java.util.Map;

public class ProcessFilter implements Filter {

    @Override
    public Object process(Object input) {
        if (input == null) {
            return null;
        }

        String content = (String) input;
        Map<String, Integer> wordCount = new HashMap<>();

        // 清理文本并分割单词
        String[] words = content.toLowerCase()
                .replaceAll("[^a-zA-Z0-9\\s]", " ")
                .split("\\s+");

        // 统计单词出现次数
        for (String word : words) {
            if (!word.trim().isEmpty()) {
                wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
            }
        }

        System.out.println("处理过滤器：完成单词统计，共 " + words.length + " 个单词");
        return new StatisticsResult(wordCount, words.length);
    }
}

